/**
 ******************************************************************************
 * @file	watch_ls.c
 * @brief	TR700稳定光源测试与使用
 *
 * @section Platform
 * 	-#
 * @section Library
 * -# spidev.ko
 * -# libstm32-spi.so
 *
 * - 2017-6-22,MenglongWu,MenglongWoo@aliyun.com
 *
 * Copyright (C) 2017 桂林聚联科技有限公司, Inc. All Rights Reserved.
 */

/*

debug:off |


```
[-9.96dBm]  ADCs: 361  DACs:450  ADCc: 359  DACc: 610
```

按下
*/


#include <stdio.h>
#include <string.h>

#include "printl.h"
#include <math.h>
#include <unos_input.h>
#include <cui.h>

#include "stm32-spi.h"
#include "any-chip.h"


#define CONFIG_USE_U_MATH (1)


struct _watch_ls_private
{
    float	  *dbm;
    unsigned int   *mode;
    unsigned int   *ch;
    unsigned short *s_adc;
    unsigned short *s_dac;
    unsigned short *c_adc;
    unsigned short *c_dac;
};
#define COUNT_CONTROL (3)
static void _draw(unsigned int select, void *v)
{
    // printf("asdasdf\r\n");
    struct _watch_ls_private *p                     = (struct _watch_ls_private *)v;
    char                     *c_item[COUNT_CONTROL] = { C_NORMAL, C_NORMAL, C_NORMAL };
    char                     *strmode[]             = { "cw", "270", "1k", "2k" };
    char                     *strch[]               = { "-", "1", "2", "3" };
    char                     *pmode;
    char                     *pch;

    c_item[select % ARRAY_SIZE(c_item)] = (C_CYAN AC_REVERSE);  // 高亮选中列

    pmode = strmode[*(p->mode) % sizeof(*strmode)];
    pch   = strch[*(p->ch) % sizeof(*strch)];


    printf("  %s%6.2fdBm  "
           "%s%3s  "
           "%sCh%s   " C_NORMAL "ADCs:%4d  "
           "DACs:%4d  "
           "ADCc:%4d  "
           "DACc:%4d\r",

           c_item[0],
           *(p->dbm),
           c_item[1],
           pmode,
           c_item[2],
           pch,
           *(p->s_adc),
           *(p->s_dac),
           *(p->c_adc),
           *(p->c_dac));
}

static void _key_process(unsigned int *select, void *v, int key)
{
    struct _watch_ls_private *p     = (struct _watch_ls_private *)v;
    static int                times = 0;

    int   deta     = 1;
    float deta_dbm = 0.01;
    int   tmp;


    if (0 /* && is_fastkeydown(key)*/) {
	if (times++ >= 8) {
	    deta     = 10;
	    deta_dbm = 0.10;
	}
    }
    else {
	times    = 0;
	deta     = 1;
	deta_dbm = 0.01;
    }

    switch (key) {
    case VK_DOWN:
    case 's':
	deta_dbm = -deta_dbm;
	deta     = -deta;
    case VK_UP:
    case 'w':
	switch ((unsigned int)(*select) % COUNT_CONTROL) {
	case 0:  // dbm
	    *(p->dbm) += deta_dbm;
	    if (*(p->dbm) > COUNT_CONTROL) {
		*(p->dbm) = -20;
	    }
	    else if (*(p->dbm) < -20) {
		*(p->dbm) = 3;
	    }
	    break;
	case 1:  // mode
	    *(p->mode) += deta;
	    // *(p->mode) %= LASER_MODE_COUNT;
	    break;
	case 2:  // ch
	    *(p->ch) += deta;
	    // *(p->ch) %= CTRL_CH_COUNT;
	    break;
	default:
	    break;
	}
	break;
    case VK_RIGHT:
    case 'd':
	(*select)++;
	break;
    case VK_LEFT:
    case 'a':
	(*select)--;
	break;
    default:
	break;
    }
}
#define TASK_DETA_READKEY (20 * _1MS)
#define TASK_DETA_UPDATE  (20 * _1MS)

void watch_ls(struct stm32_spi_dev *dev)
{
    // 1000 * COUNT_CONTROL 多少都无所谓，只不过如果设置为0
    unsigned int  select = 1000 * COUNT_CONTROL;
    int           key    = 0;
    unsigned long task_readkey;
    unsigned long task_update_dac;

    float                    dbm   = -10;
    unsigned int             mode  = 0;  // LASER_MODE_CW;
    unsigned int             ch    = 0;  // CTRL_CH1;
    unsigned short           unuse = 0;
    unsigned short           adc   = 0;
    struct _watch_ls_private draw;

    struct tr700_setls_tx input;

    input.dbm  = -12.0;
    input.mode = 0;
    input.ch   = 1;

    dbm  = input.dbm;
    mode = input.mode;
    ch   = input.ch;

    draw.dbm   = &dbm;
    draw.mode  = &mode;
    draw.ch    = &ch;
    draw.s_dac = &unuse;
    draw.s_adc = &unuse;
    draw.s_dac = &unuse;
    draw.c_adc = &adc;
    draw.c_dac = &unuse;


    printf(C_CYAN
           "[q]:Quit  ['w'/up , 's'/down]:Edit  "
           "['a'/left , 's'/right']:move cursor \r\n" C_NORMAL);
    task_readkey    = jiffies + TASK_DETA_READKEY;
    task_update_dac = jiffies + TASK_DETA_UPDATE;
    // setvbuf(stdout, "", _IONBF, 0);
    // set_keypress();
    // reset_keypress();
    // kbhit();
    stm_setls(dev, &input);
    _draw(select, &draw);
    while (key != 'q') {
	if (time_after(jiffies, task_readkey)) {
	    task_readkey = jiffies + TASK_DETA_READKEY;
	    // key = getkeycode();
	    key = getchar();
	    if (key > 0) {
		_key_process(&select, &draw, key);

		input.dbm  = dbm;
		input.mode = mode;
		input.ch   = ch;
		stm_setls(dev, &input);
		_draw(select, &draw);
	    }
	}
	if (time_after(jiffies, task_update_dac)) {
	    task_update_dac = jiffies + TASK_DETA_UPDATE;
	    _draw(select, &draw);
	}
    }
    printf("\r\n");
}
